home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / test / Ethernet / tcp.c < prev   
Encoding:
C/C++ Source or Header  |  1999-07-26  |  5.1 KB  |  184 lines

  1. /*
  2.  *  $Id: tcp.c,v 1.1.1.1 1999/05/18 15:33:43 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  tcp.c - Build a TCP packet at the link layer
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../../include/config.h"
  36. #endif
  37. #include "../libnet_test.h"
  38.  
  39. int
  40. main(int argc, char *argv[])
  41. {
  42.     int  c;
  43.     u_long src_ip, dst_ip;
  44.     u_short src_prt, dst_prt;
  45.     u_char *cp;
  46.     char errbuf[256];
  47.     char *device = NULL;
  48.     struct link_int *l;
  49.  
  50.     printf("link layer TCP packet building/writing test\n");
  51.  
  52.     src_ip  = 0;
  53.     dst_ip  = 0;
  54.     src_prt = 0;
  55.     dst_prt = 0;
  56.     while ((c = getopt(argc, argv, "i:d:s:")) != EOF)
  57.     {
  58.         switch (c)
  59.         {
  60.             /*
  61.              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
  62.              *  point cp to the last dot of the IP address/port string and
  63.              *  then seperate them with a NULL byte.  The optarg now points to
  64.              *  just the IP address, and cp points to the port.
  65.              */
  66.             case 'd':
  67.                 if (!(cp = strrchr(optarg, '.')))
  68.                 {
  69.                     usage(argv[0]);
  70.                 }
  71.                 *cp++ = 0;
  72.                 dst_prt = (u_short)atoi(cp);
  73.                 if (!(dst_ip = libnet_name_resolve(optarg, 1)))
  74.                 {
  75.                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
  76.                     exit(1);
  77.                 }
  78.                 break;
  79.             case 's':
  80.                 if (!(cp = strrchr(optarg, '.')))
  81.                 {
  82.                     usage(argv[0]);
  83.                 }
  84.                 *cp++ = 0;
  85.                 src_prt = (u_short)atoi(cp);
  86.                 if (!(src_ip = libnet_name_resolve(optarg, 1)))
  87.                 {
  88.                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
  89.                     exit(1);
  90.                 }
  91.                 break;
  92.             case 'i':
  93.                 device = optarg;
  94.                 break;
  95.             default:
  96.                 exit(EXIT_FAILURE);
  97.         }
  98.     }
  99.  
  100.     if (!src_ip || !src_prt || !dst_ip || !dst_prt || !device)
  101.     {
  102.         usage(argv[0]);
  103.         exit(EXIT_FAILURE);
  104.     }
  105.  
  106.     if ((l = libnet_open_link_interface(device, errbuf)) == NULL)
  107.     {
  108.         fprintf(stderr, "libnet_open_link_interface: %s\n", errbuf);
  109.         exit(EXIT_FAILURE);
  110.     }
  111.     c = send_tcp(l, device, src_ip, src_prt, dst_ip, dst_prt);
  112.  
  113.     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
  114. }
  115.  
  116.  
  117. int
  118. send_tcp(struct link_int *l, u_char *device, u_long src_ip, u_short src_prt,
  119.         u_long dst_ip, u_short dst_prt)
  120. {
  121.     int n;
  122.     u_char *buf;
  123.  
  124.     if (libnet_init_packet(TCP_H + IP_H + ETH_H, &buf) == -1)
  125.     {
  126.         perror("no packet memory");
  127.         exit(EXIT_FAILURE);
  128.     }
  129.  
  130.     /*
  131.      *  Ethernet header
  132.      */
  133.     libnet_build_ethernet(enet_dst, enet_src, ETHERTYPE_IP, NULL, 0, buf);
  134.  
  135.     libnet_build_ip(TCP_H,
  136.         0,
  137.         242,
  138.         0,
  139.         64,
  140.         IPPROTO_TCP,
  141.         src_ip,
  142.         dst_ip,
  143.         NULL,
  144.         0,
  145.         buf + ETH_H);
  146.  
  147.     libnet_build_tcp(src_prt,
  148.         dst_prt,
  149.         111111,
  150.         999999,
  151.         TH_SYN,
  152.         32767,
  153.         0,
  154.         NULL,
  155.         0,
  156.         buf + IP_H + ETH_H);
  157.  
  158.     libnet_do_checksum(buf + ETH_H, IPPROTO_IP, IP_H);
  159.     libnet_do_checksum(buf + ETH_H, IPPROTO_TCP, TCP_H);
  160.  
  161.     n = libnet_write_link_layer(l, device, buf, ETH_H + IP_H + TCP_H);
  162.     if (n != ETH_H + IP_H + TCP_H)
  163.     {
  164.         fprintf(stderr, "Oopz.  Only wrote %d bytes\n", n);
  165.     }
  166.     else
  167.     {
  168.         printf("Wrote %d byte TCP packet through linktype %d\n", n, l->linktype);
  169.     }
  170.     libnet_destroy_packet(&buf);
  171.     return (n);
  172. }
  173.  
  174. void
  175. usage(u_char *name)
  176. {
  177.     fprintf(stderr,
  178.         "usage: %s -i interface -s source_ip.source_port"
  179.         " -d destination_ip.destination_port\n",
  180.         name);
  181. }
  182.  
  183. /* EOF */
  184.